﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Zhy.DigitaPlatform.Components
{
    /// <summary>
    /// 所有组件的父类
    /// 支持放大缩小功能， 
    /// </summary>
    public class ComponentBase : UserControl
    {
        /// <summary>
        /// 删除命令
        /// </summary>
        public ICommand DeleteCommand
        {
            get { return (ICommand)GetValue(DeleteCommandProperty); }
            set { SetValue(DeleteCommandProperty, value); }
        }
        /// <summary>
        /// 删除命令依赖属性
        /// </summary>
        public static readonly DependencyProperty DeleteCommandProperty =
            DependencyProperty.Register("DeleteCommand", typeof(ICommand), typeof(ComponentBase),
                new PropertyMetadata(null, new PropertyChangedCallback((d, e) =>
                {
                })));

        /// <summary>
        /// 删除参数
        /// </summary>
        public object DeleteParameter
        {
            get { return (object)GetValue(DeleteParameterProperty); }
            set { SetValue(DeleteParameterProperty, value); }
        }
        /// <summary>
        /// 删除参数依赖属性
        /// </summary>
        public static readonly DependencyProperty DeleteParameterProperty =
            DependencyProperty.Register("DeleteParameter", typeof(object), typeof(ComponentBase), new PropertyMetadata(null));

        /// <summary>
        /// 组件选中属性
        /// </summary>
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
        /// <summary>
        /// 组件选中依赖属性
        /// </summary>
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ComponentBase), new PropertyMetadata(false));


        ///// <summary>
        ///// 显示宽度
        ///// </summary>
        //public double ShowWidth
        //{
        //    get { return (double)GetValue(ShowWidthProperty); }
        //    set { SetValue(ShowWidthProperty, value); }
        //}
        ///// <summary>
        ///// 显示宽度依赖属性
        ///// </summary>
        //public static readonly DependencyProperty ShowWidthProperty =
        //    DependencyProperty.Register("ShowWidth", typeof(double), typeof(ComponentBase), new PropertyMetadata(0.0));

        ///// <summary>
        ///// 显示高度
        ///// </summary>
        //public double ShowHeight
        //{
        //    get { return (double)GetValue(ShowHeightProperty); }
        //    set { SetValue(ShowHeightProperty, value); }
        //}
        ///// <summary>
        ///// 显示高度依赖属性
        ///// </summary>
        //public static readonly DependencyProperty ShowHeightProperty =
        //    DependencyProperty.Register("ShowHeight", typeof(double), typeof(ComponentBase), new PropertyMetadata(0.0));

        /// <summary>
        /// 组件的鼠标按下命令
        /// </summary>
        public ICommand ResizeDownCommand
        {
            get { return (ICommand)GetValue(ResizeDownCommandProperty); }
            set { SetValue(ResizeDownCommandProperty, value); }
        }
        public static readonly DependencyProperty ResizeDownCommandProperty =
            DependencyProperty.Register("ResizeDownCommand", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(null));

        /// <summary>
        /// 鼠标的移动命令
        /// </summary>
        public ICommand ResizeMoveCommand
        {
            get { return (ICommand)GetValue(ResizeMoveCommandProperty); }
            set { SetValue(ResizeMoveCommandProperty, value); }
        }
        public static readonly DependencyProperty ResizeMoveCommandProperty =
            DependencyProperty.Register("ResizeMoveCommand", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(null));
        /// <summary>
        /// 鼠标的抬起命令
        /// </summary>
        public ICommand ResizeUpCommand
        {
            get { return (ICommand)GetValue(ResizeUpCommandProperty); }
            set { SetValue(ResizeUpCommandProperty, value); }
        }
        public static readonly DependencyProperty ResizeUpCommandProperty =
            DependencyProperty.Register("ResizeUpCommand", typeof(ICommand), typeof(ComponentBase), new PropertyMetadata(null));
        /// <summary>
        /// 组件旋转角度
        /// </summary>
        public int RotateAngle
        {
            get { return (int)GetValue(RotateAngleProperty); }
            set { SetValue(RotateAngleProperty, value); }
        }
        /// <summary>
        /// 组件旋转角度
        /// </summary>
        public static readonly DependencyProperty RotateAngleProperty =
            DependencyProperty.Register("RotateAngle", typeof(int), typeof(ComponentBase), new PropertyMetadata(0));

        /// <summary>
        /// 组件管道流向
        /// </summary>
        public int FlowDirection
        {
            get { return (int)GetValue(FlowDirectionProperty); }
            set { SetValue(FlowDirectionProperty, value); }
        }
        /// <summary>
        /// 组件管道流向
        /// </summary>
        public static readonly DependencyProperty FlowDirectionProperty =
            DependencyProperty.Register("FlowDirection", typeof(int), typeof(ComponentBase), new PropertyMetadata(0, (d, e) =>
            {
                var state = VisualStateManager.GoToState(d as ComponentBase, e.NewValue.ToString() == "1" ? "EWFlowState" : "WEFlowState", false);
            }));
        /// <summary>
        /// 警告状态
        /// </summary>
        public bool IsWarning
        {
            get { return (bool)GetValue(IsWarningProperty); }
            set { SetValue(IsWarningProperty, value); }
        }
        /// <summary>
        /// 警告状态
        /// </summary>
        public static readonly DependencyProperty IsWarningProperty =
            DependencyProperty.Register("IsWarning", typeof(bool), typeof(ComponentBase), new PropertyMetadata(false, (d, e) =>
            {   // 监听警告状态-当警告状态变了需要处理的逻辑
                if (e.NewValue.ToString() != e.OldValue.ToString()) // 如果新的属性值不等于更改之前的属性值判断
                    // 控制动画效果
                    VisualStateManager.GoToState(d as ComponentBase, (bool)e.NewValue ? "WarningState" : "NormalState", false);
            }));

        /// <summary>
        /// 警告信息
        /// </summary>
        public string WarningMessage
        {
            get { return (string)GetValue(WarningMessageProperty); }
            set { SetValue(WarningMessageProperty, value); }
        }
        /// <summary>
        /// 警告信息
        /// </summary>
        public static readonly DependencyProperty WarningMessageProperty =
            DependencyProperty.Register("WarningMessage", typeof(string), typeof(ComponentBase), new PropertyMetadata(""));

        #region 组件拖动相关事件
        /// <summary>
        /// 组件选中标识-用于组件是否正在拖动
        /// </summary>
        bool is_move = false;
        /// <summary>
        /// 
        /// </summary>
        Point start = new Point(0, 0);
        /// <summary>
        /// 组件拖动事件-鼠标左键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //// 修改ShwoWidth/ShowHeight
            //is_move = true;

            //// 获取相对Canvas的按下坐标
            //start = e.GetPosition(GetParent(this));
            //Mouse.Capture((IInputElement)sender); // 鼠标捕获组件元素-用于拖动组件时判断元素类型
            //e.Handled = true; // 用于阻断路由事件的冒泡排序

            ResizeDownCommand?.Execute(e);
        }
        /// <summary>
        /// 组件拖动事件-鼠标左键松开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //// 修改ShwoWidth/ShowHeight
            //is_move = false; // 鼠标松开拖动完成
            //e.Handled = true; // 用于阻断路由事件的冒泡排序
            //Mouse.Capture(null); // 释放鼠标捕获元素

            ResizeUpCommand?.Execute(e);
        }
        /// <summary>
        /// 组件拖动事件-鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Ellipse_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeMoveCommand?.Execute(e);

            //// 修改ShwoWidth/ShowHeight
            //if (is_move) // 组件选中正在拖动
            //{
            //    // 鼠标光标的新位置
            //    Point current = e.GetPosition(GetParent(this));
            //    // 根据光标类型判断是如何变化 
            //    var c = (sender as Ellipse).Cursor;
            //    if (c != null)
            //    {
            //        if (c == Cursors.SizeWE)// 水平方向
            //        {
            //            this.ShowWidth += current.X - start.X;
            //        }
            //        else if (c == Cursors.SizeNS)// 垂直方向
            //        {
            //            this.ShowHeight += current.Y - start.Y;
            //        }
            //        else if (c == Cursors.SizeNWSE)// 右下方向
            //        {
            //            if (Keyboard.Modifiers == ModifierKeys.Control)
            //            {
            //                // 没有锁定（如果拖动的时候同时按下了Ctrl键 ）
            //                this.ShowWidth += current.X - start.X;
            //                this.ShowHeight += current.Y - start.Y;
            //            }
            //            else
            //            {
            //                // 锁定比例
            //                var rate = this.ShowWidth / this.ShowHeight;
            //                this.ShowWidth += current.X - start.X;
            //                this.ShowHeight = this.ShowWidth / rate;
            //            }
            //        }
            //        start = current;
            //    }
            //    e.Handled = true; // 用于阻断路由事件的冒泡排序
            //}
        }
        /// <summary>
        /// 组件拖动事件-查找父容器
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        //private Canvas GetParent(DependencyObject d)
        //{
        //    var obj = VisualTreeHelper.GetParent(d);
        //    if (obj != null && obj is Canvas)
        //        return obj as Canvas;

        //    return GetParent(obj);
        //}
        #endregion

        /// <summary>
        /// 执行住建删除命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            DeleteCommand?.Execute(DeleteParameter);
        }
    }
}
